package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.ErpStockTransferProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ErpStockTransferMapper;
import com.ruoyi.system.domain.ErpStockTransfer;
import com.ruoyi.system.service.IErpStockTransferService;

/**
 * 库存调拨Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-07
 */
@Service
public class ErpStockTransferServiceImpl implements IErpStockTransferService
{
    @Autowired
    private ErpStockTransferMapper erpStockTransferMapper;

    /**
     * 查询库存调拨
     *
     * @param id 库存调拨主键
     * @return 库存调拨
     */
    @Override
    public ErpStockTransfer selectErpStockTransferById(Long id) {
        ErpStockTransfer transfer = erpStockTransferMapper.selectErpStockTransferById(id);
        if (transfer != null) {
            // 查询关联的商品明细
            List<ErpStockTransferProduct> products = erpStockTransferMapper.selectTransferProductByTransferId(id);
            transfer.setProductList(products);
        }
        return transfer;
    }

    /**
     * 查询库存调拨列表
     *
     * @param erpStockTransfer 库存调拨
     * @return 库存调拨
     */
    @Override
    public List<ErpStockTransfer> selectErpStockTransferList(ErpStockTransfer erpStockTransfer)
    {
        return erpStockTransferMapper.selectErpStockTransferList(erpStockTransfer);
    }

    /**
     * 新增库存调拨
     *
     * @param erpStockTransfer 库存调拨
     * @return 结果
     */
    @Override
    public int insertErpStockTransfer(ErpStockTransfer erpStockTransfer) {
        erpStockTransfer.setCreateTime(DateUtils.getNowDate());
        if (erpStockTransfer.getStatus() == null) {
            erpStockTransfer.setStatus("未提交");
        }

        // 1. 先插入调拨单
        int result = erpStockTransferMapper.insertErpStockTransfer(erpStockTransfer);

        // 2. 获取调拨单ID
        Long transferId = erpStockTransfer.getId();

        // 3. 插入商品明细（确保 stock_transfer_id 被赋值）
        if (result > 0 && erpStockTransfer.getProductList() != null) {
            for (ErpStockTransferProduct product : erpStockTransfer.getProductList()) {
                product.setStockTransferId(transferId); // 关键：设置关联ID
                if (product.getId() == null) {
                    product.setId(IdUtils.generateLongId());
                }
                erpStockTransferMapper.insertTransferProduct(product);
            }
        }
        return result;
    }

    /**
     * 修改库存调拨
     *
     * @param erpStockTransfer 库存调拨
     * @return 结果
     */
    @Override
    public int updateErpStockTransfer(ErpStockTransfer erpStockTransfer) {
        erpStockTransfer.setUpdateTime(DateUtils.getNowDate());

        // 先更新主表
        int result = erpStockTransferMapper.updateErpStockTransfer(erpStockTransfer);

        // 处理商品明细
        if (result > 0 && erpStockTransfer.getProductList() != null) {
            // 先删除旧的明细
            erpStockTransferMapper.deleteTransferProductByTransferId(erpStockTransfer.getId());

            // 插入新的明细
            for (ErpStockTransferProduct product : erpStockTransfer.getProductList()) {
                product.setStockTransferId(erpStockTransfer.getId()); // 确保设置关联ID
                if (product.getId() == null) {
                    product.setId(IdUtils.generateLongId());
                }
                erpStockTransferMapper.insertTransferProduct(product);
            }
        }
        return result;
    }

    /**
     * 批量删除库存调拨
     *
     * @param ids 需要删除的库存调拨主键
     * @return 结果
     */
    @Override
    public int deleteErpStockTransferByIds(Long[] ids)
    {
        return erpStockTransferMapper.deleteErpStockTransferByIds(ids);
    }

    /**
     * 删除库存调拨信息
     *
     * @param id 库存调拨主键
     * @return 结果
     */
    @Override
    public int deleteErpStockTransferById(Long id)
    {
        return erpStockTransferMapper.deleteErpStockTransferById(id);
    }

    @Override
    public int submitTransfer(Long id) {
        return erpStockTransferMapper.updateStatusToSubmitted(id);
    }

    @Override
    public int withdrawTransfer(Long id) {
        return erpStockTransferMapper.updateStatusToUnsubmitted(id);
    }

    @Override
    public int confirmTransfer(Long id) {
        return erpStockTransferMapper.updateStatusToConfirmed(id);
    }

    @Override
    public List<Map<String, Object>> selectGoodsByWarehouse(Long warehouseId) {
        return erpStockTransferMapper.selectGoodsByWarehouse(warehouseId);
    }
}
